package com.vmc.util;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import com.vmc.exception.VMCException;
import com.vmc.model.AbstractTransferDetail;
import com.vmc.model.DownloadTransferProperties;
import com.vmc.model.MovieTransferDetailList;

public class GlobalStorageUtility {
	static Logger logger = LogManager.getLogger(GlobalStorageUtility.class);

	public static final String PROPERTIES_FILE = "./app.properties"; 
	public static final String ALTERNATE_PROPERTIES_FILE = "./app/config/app.properties"; 

	private static GlobalStorageUtility INSTANCE = new GlobalStorageUtility();
	
	private DownloadTransferProperties properties;
	private MovieTransferDetailList movieTransferList;
	private List<AbstractTransferDetail> copiedFiles;
	private List<File> unsuccessfulTransfers;
	
	private GlobalStorageUtility() {
		
	}
	
	public static GlobalStorageUtility getInstance() {
		return INSTANCE;
	}
	
	public DownloadTransferProperties getProperties() {
		return properties;
	}

	public void setProperties(DownloadTransferProperties properties) {
		this.properties = properties;
	}

	public MovieTransferDetailList getMovieTransferList() {
		return movieTransferList;
	}

	public void setMovieTransferList(MovieTransferDetailList movieTransferList) {
		this.movieTransferList = movieTransferList;
	}
	
	public List<AbstractTransferDetail> getCopiedFiles() {
		return this.copiedFiles;
	}
	
	public List<File> getUnsuccessfulTransfers() {
		return unsuccessfulTransfers;
	}

	public void addCopiedFile(AbstractTransferDetail detail) {
		if (copiedFiles == null) {
			copiedFiles = new ArrayList<AbstractTransferDetail>();
		}
		logger.info("Adding copied file " + detail.getDestinationFilePath() + ".");
		copiedFiles.add(detail);
		logger.info("Done adding copied file.");
	}
	
	public void addUnsuccessfulTransfer(File file) {
		if (unsuccessfulTransfers == null) {
			unsuccessfulTransfers = new ArrayList<File>();
		}
		logger.info("Adding unsuccessful transfer " + file.getAbsolutePath() + ".");
		unsuccessfulTransfers.add(file);
		logger.info("Done adding unsuccessful transfer.");
	}

	public void initialize() throws VMCException {
		// perform all initializations here
		logger.info("Initializing...");
		this.generateDownloadTransferProperties();
		this.generateMovieTransferDetailList();
		logger.info("Initialization done.");
	}
	
	private void generateMovieTransferDetailList() {
		logger.info("Loading Movie Transfer Details...");
		if (this.getProperties() != null && this.getProperties().getMovieConversionDirectory() != null) {
			this.setMovieTransferList(MovieTransferDetailList.Builder.build(this.getProperties().getMovieConversionDirectory()));
		}
		logger.info("Done loading Movie Transfer Details.");
	}
	
	private void generateDownloadTransferProperties() throws VMCException {
		logger.info("Loading DownloadTransfer properties...");
		this.setProperties(DownloadTransferProperties.Builder.build(PROPERTIES_FILE, ALTERNATE_PROPERTIES_FILE));
		logger.info("Done!");
		logger.info(properties);
	}
}
